﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RN.ERP.Web.BasicService;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.EnterpriseService;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// AreaHandler 的摘要说明
    /// </summary>
    public class AreaHandler : BaseHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            string city = context.Request["city"] ?? "0";
            ////是否只检索省内市级地区
            string OnlyContainsProvinceCity = context.Request["isProvince"];
            ////是否排除已有市场范围
            string IsRemoveExistEmbranchArea = context.Request["isRemoveExist"];
            ////特殊排除的地区
            string exceptAreas = context.Request["exceptAreas"];
            ////专家分库type=
            string type = context.Request["type"];
            string proStr = null;
            BasicServiceClient client = new BasicServiceClient();

            if (type == "expert")
            {
                ////专家分库信息
                proStr = context.Application[ApplicationConstant.APPLICATION_EXPERTDBCODE].ToString();
                List<T_Sys_Dict> list = JsonConvert.DeserializeObject<List<T_Sys_Dict>>(proStr);
                if (city == "0")
                {
                    ////获取一级
                    proStr = JsonConvert.SerializeObject(list.Where(m => m.ParentSID == 0));
                }
                else
                {
                    ////获取二级
                    int pSID = 0;
                    int.TryParse(city, out pSID);
                    proStr = JsonConvert.SerializeObject(list.Where(m => m.ParentSID == pSID));
                }
            }
            else if (city == "0")
            {
                proStr = context.Application[ApplicationConstant.APPLICATION_AREA].ToString();
                List<a_AreaInfo> list = JsonConvert.DeserializeObject<List<a_AreaInfo>>(proStr);
                if (OnlyContainsProvinceCity == "1")
                {
                    list = list.Where(l => l.Code != "18").ToList();
                }
                if (IsRemoveExistEmbranchArea == "1")
                {
                    string corpUserGuid = string.Empty;
                    if (string.IsNullOrEmpty(corpUserGuid))
                    {
                        corpUserGuid = UserInfo.CorpUserGuid;
                    }
                    Tb_Corp_CompanyInfo corp = new Tb_Corp_CompanyInfo();
                    Guid guid = Guid.Empty;
                    if (Guid.TryParse(corpUserGuid, out guid))
                    {
                        EnterpriseServiceClient enterpriseServiceClient = new EnterpriseServiceClient();
                        corp.CorpUserGuid = guid;
                        var Embranchments = enterpriseServiceClient.GetCorpEmbranchments(JsonConvert.SerializeObject(corp));
                        var CorpEmbranchments = JsonConvert.DeserializeObject<IList<Tb_Corp_Embranchment>>(Embranchments);
                        var queryExistAreas = from e in CorpEmbranchments
                                              select e.AreaCode;
                        var query = from l in list
                                    where !queryExistAreas.Contains(l.Code)
                                    select l;
                        list = query.ToList();
                    }
                }
                if (!string.IsNullOrEmpty(exceptAreas))
                {
                    string[] areas = exceptAreas.Split(',');
                    list = list.Where(l => !areas.Contains(l.Code)).ToList();
                }
                proStr = JsonConvert.SerializeObject(list);
            }
            else if (type == "ZBDL")
            {
                proStr = client.GetDictionary("areassq", " and  ParentCode='" + city + "' and code like '" + city + "%' and  ssqFlag=3", "");
            }
            else
            {
                proStr = client.GetQu(city);
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(proStr);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
